package net.zhangdaqian.lightDom;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;


public class AutoMark {
	LightDocument abstractDocument = new LightDocument();
	LightDocument[] sampleDocument;

	private void initSampleDocument(List<String> paths) {
		abstractDocument = new LightDocument();
		sampleDocument = new LightDocument[paths.size()];
		{
			FileReader fr = null;
			for (int i = 0; i < paths.size(); i++) {
				sampleDocument[i] = new LightDocument();
				try {
					fr = new FileReader(paths.get(i));
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				sampleDocument[i].parse(fr);
			}
		}
		// return abstractDocument.all().innerHtml().toString();
	}

	public LightDocument abstractTinyDocument(List<String> paths) {
		initSampleDocument(paths);
		LightDocument referDocument = null;
		int abstractint = 1;
		for (int i = 0; i < sampleDocument.length; i++) {
			if (referDocument == null) {
				referDocument = sampleDocument[i];
			} else {
				if (referDocument.getMaxLevel() > sampleDocument[i]
						.getMaxLevel()) {
					referDocument = sampleDocument[i];
					abstractint = i;
				}
			}
		}
		for (int i = 0; i < referDocument.getElementsByLevel(1).length(); i++) {
			abstractEachElement(referDocument.getElementsByLevel(1).get(i));
		}
		try {
			abstractDocument = referDocument.clone();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		abstractDocument.getElementsCollect().sortByInnerId();
		{
			for (Iterator<Element> iterator = abstractDocument
					.getElementsCollect().elements.iterator(); iterator
					.hasNext();) {
				Element element = (Element) iterator.next();
				if (element.s_FreeToUse != null
						&& element.s_FreeToUse.indexOf("mark") < 0) {
					iterator.remove();
				}
			}
		}
		return abstractDocument;
	}

	private void abstractEachElement(Element element) {
		int count = 0;
		for (int k = 0; k < sampleDocument.length; k++) {
			if (sampleDocument[k].getElementsCollect().indexOf(element) >= 0) {
				count++;
			}
		}
		if (count > (sampleDocument.length * 0.6)) {
			element.s_FreeToUse += "mark";
		}
		element.s_FreeToUse += "parse";
		ElementsCollect elementsCollect = element.getChildNodes();
		// if (elementsCollect.length() > 0) {
		for (Iterator<Element> iterator = elementsCollect.elements.iterator(); iterator
				.hasNext();) {
			Element child = (Element) iterator.next();
			abstractEachElement(child);
		}

	}

	public static void main(String[] args) {
		List<String> paths = new LinkedList<String>();
		paths.add("e:\\test\\mark\\1.txt");
		paths.add("e:\\test\\mark\\2.txt");
		paths.add("e:\\test\\mark\\3.txt");
		List<String> samepaths = new LinkedList<String>();
		samepaths.add("e:\\test\\mark\\same1.txt");
		samepaths.add("e:\\test\\mark\\same2.txt");
		samepaths.add("e:\\test\\mark\\same3.txt");
		AutoMark autoMark = new AutoMark();
		LightDocument lightDocument = autoMark.abstractTinyDocument(paths);
		lightDocument.innerHtml();
		Element element = new Element();
		element.setTagName("style");
		element.setContent(".tinyDomMark{border-style: solid ;}");
		lightDocument.addElementAfter(lightDocument.getElementByTagName("head")
				.get(0), element);
		lightDocument.getElementByTagName("body").get(0).setAttribute("class",
				"tinyDomMark");

		lightDocument.writeToFile("e:\\test\\mark\\abstract.htm");
	}
}
